import { contextBridge, ipcRenderer } from 'electron'
import { electronAPI } from '@electron-toolkit/preload'
import path from 'path'
import os from 'os'

// Custom APIs for renderer
const api = {
	getFilePath:(url)=>{
		return path.resolve(__dirname,url)
	}
}

// Use `contextBridge` APIs to expose Electron APIs to
// renderer only if context isolation is enabled, otherwise
// just add to the DOM global.
console.log('process.contextIsolated',process.contextIsolated)
if (process.contextIsolated) {
	try {
		contextBridge.exposeInMainWorld('electron', electronAPI)
		contextBridge.exposeInMainWorld('api', api)
		contextBridge.exposeInMainWorld('native', {
			openFile: () => ipcRenderer.invoke('main:openFile'),
			openNewWindow: (data) => ipcRenderer.invoke('main:openNewWindow',data),
			openUrl: (data) => ipcRenderer.invoke('main:openUrl',data),
		})
	} catch (error) {
		console.error(error)
	}
} else {
	// @ts-ignore (define in dts)
	window.electron = electronAPI
	// @ts-ignore (define in dts)
	window.api = api
}
